﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace TeamCompare
{
	public class SelfLabeledTextBox: TextBox
	{
		public static readonly DependencyProperty EmptyBoxTextProperty =
			DependencyProperty.Register("EmptyBoxText", typeof(string), typeof(SelfLabeledTextBox), new PropertyMetadata(_emtyBoxText, EmptyBoxTextChanged));

		private const string _emtyBoxText = "Enter value";

		/// <summary>
		/// Текст в пустом текстовом блоке.
		/// </summary>
		public string EmptyBoxText
		{
			get 
			{
				return (string)GetValue(EmptyBoxTextProperty);
			}
			set 
			{
				SetValue(EmptyBoxTextProperty, value);
			}
		}

		private static void EmptyBoxTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			if (String.IsNullOrEmpty(((TextBox)d).Text) || ((TextBox)d).Text == e.NewValue.ToString())
				((TextBox)d).Text = e.NewValue.ToString();
		}

		public SelfLabeledTextBox()
		{
			StyleAsEmpty();

			GotFocus += SelfLabeledTextBoxGotFocus;
			LostFocus += SelfLabeledTextBoxLostFocus;
		}

		void SelfLabeledTextBoxLostFocus(object sender, RoutedEventArgs e)
		{
			if (String.IsNullOrEmpty(this.Text) || this.Text == EmptyBoxText)
			{
				StyleAsEmpty();
				this.Text = EmptyBoxText;
			}
		}

		void SelfLabeledTextBoxGotFocus(object sender, RoutedEventArgs e)
		{
			if (String.IsNullOrEmpty(this.Text) || this.Text == EmptyBoxText)
			{
				StyleAsFilled();
				this.Text = String.Empty;
			}
		}

		public void StyleAsEmpty()
		{
			this.Foreground = new SolidColorBrush(Colors.Gray);
		}

		public void StyleAsFilled()
		{
			this.Foreground = new SolidColorBrush(Colors.Black);
		}
		


	}
}
